﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenGL;
using BF;
using OSM;

namespace Testfield
{
	public partial class Form1 : GLForm
	{
		BF_Engine Engine = new BF_Engine();
		public Form1()
		{
			InitializeComponent();
		}

		GLText T;
		uint[] buffernames = new uint[1] { 0 };
		double[] buf;

		protected override void InitGL()
		{
			Engine.Map.LoadBorders();

			T = new GLText("Arial");
			buf = new double[360 * 178 * 12];
			int N = 0;
			for (int i = -180; i < 180; i++)
				for (int j = -89; j < 89; j++)
				{
					buf[N] = new V3G(i, j).X;
					buf[N + 1] = new V3G(i, j).Y;
					buf[N + 2] = new V3G(i, j).Z;
					buf[N + 3] = new V3G(i + 1, j).X;
					buf[N + 4] = new V3G(i + 1, j).Y;
					buf[N + 5] = new V3G(i + 1, j).Z;

					buf[N + 6] = new V3G(i, j).X;
					buf[N + 7] = new V3G(i, j).Y;
					buf[N + 8] = new V3G(i, j).Z;
					buf[N + 9] = new V3G(i, j + 1).X;
					buf[N + 10] = new V3G(i, j + 1).Y;
					buf[N + 11] = new V3G(i, j + 1).Z;

					N += 12;
				}
		}
		protected override void Process(double dt, double tt)
		{
			View.Process(dt);
			Engine.Process(dt);
		}
		PT1_V3D View = new PT1_V3D(1, 1, V3D.Black);

		HiPerfTimer T2 = new HiPerfTimer();
		protected override bool DrawGLScene()
		{
			MakePerspective(60, 10f, 400000);
			GL.glLoadIdentity();
			GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
			GL.gluLook3G(0, 0, 200000, 30);
			CalculateMouse();
			GL.glDisable(GL.GL_LIGHTING);
			T2.Start();
			if (true)
			{
				GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
				GL.glVertexPointer3d(0, buf);
				GL.glDrawArrays(GL.GL_LINES, 0, buf.Length / 3);
				GL.glDisableClientState(GL.GL_VERTEX_ARRAY);
			}
			else
			{
				GL.glBegin(GL.GL_LINES);
				for (int i = 0; i < 180; i++)
					for (int j = 0; j < 89; j++)
					{
						GL.glV3D(new V3G(i, j));
						GL.glV3D(new V3G(i, j + 1));
						GL.glV3D(new V3G(i, j));
						GL.glV3D(new V3G(i + 1, j));
					}
				GL.glEnd();
			}
			foreach (BF_Unit U in Engine.Units)
			{
				GL.glPushMatrix();
				GL.glTrans3D(U.Position);
				GL.DrawSign(10000);
				GL.glPopMatrix();
			}
			foreach (var x in Engine.Map.Borders)
			{
				GL.glBegin(GL.GL_LINE_STRIP);
				foreach (var p in x.Points)
					GL.glV3D(p);
				GL.glEnd();
			}
			T2.Stop();
			MakeOrtho();
			//T.DrawText(Engine.Units[0].Position.Longitude.ToString("0.000000"), 0, 0, StringAlignment.Near);
			//T.DrawText(Engine.Units[0].Position.Longitude.ToString("0.000000"), 0, 20, StringAlignment.Near);

			return true;
		}
		protected override void ProcessGLKey(Keys K, bool shift, bool control)
		{
			if (K == Keys.D1)
				Engine.Units[0].Move(100);
			else
				Engine.Units[0].Stop();
			if (K == Keys.D2)
				Engine.Units[1].Move(50);
			else
				Engine.Units[1].Stop();
			if (K == Keys.Left)
			{
				View.Set(View.A - V3D.Red * 10);
			}
			else if (K == Keys.Right)
				View.Set(View.A + V3D.Red * 10);
			base.ProcessGLKey(K, shift, control);
		}
		void DrawMap()
		{
		}
	}
}